#!/usr/bin/python2.4
#
#Copyright 2009 Google Inc.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
#


"""Tests for the module text_data_reader."""

import logging
import unittest
from google.appengine.ext import db
import db_utils
from metadata_in_memory import  MetadataInMemory
import text_data_reader
import digital_content_wizard_exceptions


class MetadataReaderTest(unittest.TestCase):
  """Tests for Metadata file reading."""

  def setUp(self):
    logging.info('TEST: ' + self.id())
    self.db_handler = db_utils.DbHandler()
    self.metadata_in_memory = MetadataInMemory()
    self.version_id = self.db_handler.CreateNewVersion()

  def tearDown(self):
    self.db_handler.RemoveVersion(self.version_id)
    self.metadata_in_memory.Clear()

  def testValidFile(self):
    f = open('test/data/metadata_valid.txt', 'r')
    try:
      count = text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      self.fail('InvalidMetadataException is not expected.')
    finally:
      f.close()
    self.assertEqual(count, 4)
    self.assertEqual(self.metadata_in_memory.GetNumberOfRemainingRows(), 6)

  def testInvalidFile(self):
    f = open('test/data/global_valid.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testEmptyFile(self):
    f = open('test/data/empty.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testNotTabSeparatedFile(self):
    f = open('test/data/metadata_not_tab_separated.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testUnicodeCharactersInCell(self):
    f = open('test/data/metadata_unicode_in_cell.txt', 'r')
    try:
      count = text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      self.fail('InvalidMetadataException is not expected.')
    finally:
      f.close()
    self.assertEqual(count, 4)
    self.assertEqual(self.metadata_in_memory.GetNumberOfRemainingRows(), 6)

  def testLargeTextInCell(self):
    f = open('test/data/metadata_large_text_in_cell.txt', 'r')
    try:
      count = text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      self.fail('InvalidMetadataException is not expected.')
    finally:
      f.close()
    self.assertEqual(count, 4)
    self.assertEqual(self.metadata_in_memory.GetNumberOfRemainingRows(), 6)

  def testNoHeaderRow(self):
    f = open('test/data/metadata_no_header.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testNewlineCharactersInCell(self):
    f = open('test/data/metadata_newlines_in_cell.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testTabCharactersInCell(self):
    f = open('test/data/metadata_tabs_in_cell.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    except digital_content_wizard_exceptions.InvalidContentTypeException:
      pass
    else:
      self.fail(
          'InvalidMetadataException or InvalidContentTypeException expected.')
    finally:
      f.close()

  def testJunkCharactersInCell(self):
    f = open('test/data/metadata_junk_in_cell.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    except digital_content_wizard_exceptions.InvalidContentTypeException:
      pass
    else:
      self.fail(
          'InvalidMetadataException or InvalidContentTypeException expected.')
    finally:
      f.close()

  def testNotTextFile(self):
    f = open('test/data/logo_test.gif', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testEmptyContentIds(self):
    f = open('test/data/metadata_empty_contentid.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testUniqueContentIds(self):
    f = open('test/data/metadata_repeated_contentids.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testEmptyCellsInFirstSortingColumn(self):
    f = open('test/data/metadata_empty_first_sorting.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testEmptyCellsInSecondSortingColumn(self):
    f = open('test/data/metadata_empty_second_sorting.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testEmptyCellsInThirdSortingColumn(self):
    f = open('test/data/metadata_empty_third_sorting.txt', 'r')
    try:
      count = text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      self.fail('InvalidMetadataException is not expected.')
    finally:
      f.close()
    self.assertEqual(count, 4)
    self.assertEqual(self.metadata_in_memory.GetNumberOfRemainingRows(), 6)

  def testEmptyCellsInTitleColumn(self):
    f = open('test/data/metadata_empty_title_column.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()

  def testAllEmptyCellsAfterThirdSortingColumn(self):
    f = open('test/data/metadata_empty_after_third_sorting.txt', 'r')
    try:
      text_data_reader.ReadMetadataFile(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidMetadataException:
      pass
    else:
      self.fail('InvalidMetadataException expected.')
    finally:
      f.close()


class GlobalDataReaderTest(unittest.TestCase):
  """Tests for Globaldata file reading."""

  def setUp(self):
    logging.info('TEST: ' + self.id())
    self.db_handler = db_utils.DbHandler()
    self.metadata_in_memory = MetadataInMemory()
    self.version_id = self.db_handler.CreateNewVersion()

  def tearDown(self):
    self.db_handler.RemoveVersion(self.version_id)
    self.metadata_in_memory.Clear()

  def testValidFile(self):
    f = open('test/data/global_valid.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      self.fail('InvalidGlobaldataException not expected')
    f.close()
    q = db.GqlQuery('SELECT * FROM GlobalData WHERE version_id=:1',
                    self.version_id)
    self.assertEqual(q.count(), 12)
    f = open('test/data/global_valid.txt', 'r')
    f.readline()
    variables = []
    for line in f:
      columns = line.strip().split('\t')
      variables.append(columns[2])
    f.close()
    for globaldata in q:
      self.assert_(globaldata.variable in variables)

  def testInvalidFile(self):
    f = open('test/data/metadata_valid.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testEmptyFile(self):
    f = open('test/data/empty.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testNotTabSeparatedFile(self):
    f = open('test/data/globaldata_not_tab_separated.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testUnicodeCharactersInCell(self):
    f = open('test/data/globaldata_unicode_in_cell.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      self.fail('InvalidGlobaldataException not expected.')
    f.close()
    q = db.GqlQuery('SELECT * FROM GlobalData WHERE version_id=:1',
                    self.version_id)
    self.assertEqual(q.count(), 15)
    f = open('test/data/globaldata_unicode_in_cell.txt', 'r')
    f.readline()
    variables = []
    for line in f:
      columns = line.strip().split('\t')
      variables.append(columns[2])
    f.close()
    for globaldata in q:
      self.assert_(globaldata.variable in variables)

  def testLargeTextInCell(self):
    f = open('test/data/globaldata_large_text_in_cell.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      self.fail('InvalidGlobaldataException not expected.')
    f.close()
    q = db.GqlQuery('SELECT * FROM GlobalData WHERE version_id=:1',
                    self.version_id)
    self.assertEqual(q.count(), 15)
    f = open('test/data/globaldata_large_text_in_cell.txt', 'r')
    f.readline()
    variables = []
    for line in f:
      columns = line.strip().split('\t')
      variables.append(columns[2])
    f.close()
    for globaldata in q:
      self.assert_(globaldata.variable in variables)

  def testNoHeaderRow(self):
    f = open('test/data/globaldata_no_header.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testNewlineCharactersInCell(self):
    f = open('test/data/globaldata_newlines_in_cell.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      self.fail('InvalidGlobaldataException not expected.')

  def testTabCharactersInCell(self):
    f = open('test/data/globaldata_tabs_in_cell.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testJunkCharactersInCell(self):
    f = open('test/data/globaldata_junk_in_cell.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testNotTextFile(self):
    f = open('test/data/logo_test.gif', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testOnlyThreeColumns(self):
    f = open('test/data/globaldata_more_columns.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

  def testNotVariableNameInThirdRow(self):
    f = open('test/data/globaldata_not_variable_name.txt', 'r')
    try:
      text_data_reader.GetGlobalData(f, self.version_id)
    except digital_content_wizard_exceptions.InvalidGlobaldataException:
      pass
    else:
      self.fail('InvalidGlobaldataException expected.')

if __name__ == '__main__':
  unittest.main()
